<template>
	<view>
		<view class="top">
			<up-image :src="bg" width="100%" height="700rpx"></up-image>
			
			<view class="info">
				<up-image :src="logo" height="120rpx" mode="heightFix" class="logo"></up-image>
				
				<view class="product">数字工会平台</view>
			</view>
		</view>
		
		<view class="form">
			<up-input placeholder="请输入手机号" v-model="loginForm.username">
				  <template #prefix>
					<up-text
					  text="+86"
					  margin="0 3px 0 0"
					  type="tips"
					></up-text>
				  </template>
			</up-input>
			
			<up-input placeholder="请输入密码" type="password" v-model="loginForm.password">
			</up-input>
			
			<view class="forget-wrap">
				<text>忘记密码?</text>
			</view>
			
			<up-button type="primary" text="登录" color="rgba(246, 0, 0, 1)" shape="circle" @click="login"></up-button>
			
			<up-checkbox
				:customStyle="{marginTop: '30rpx'}"
				name="agree"
				usedAlone
				@change="handleCheckboxChange"
			>
				<template #label>
					<text class="common">我已阅读并同意</text>
					<text class="highlight">《用户服务协议》</text>
					<text class="common">和</text>
					<text class="highlight">《隐私政策》</text>
				</template>
			</up-checkbox>
			
			<view class="tourist" @tap="nav2MobileLogin">手机快捷登录</view>
		</view>
	</view>
</template>

<script>
	import bg from '@/static/images/login/bg.png'
	import logo from '@/static/images/logo.png'
	// import { loginApi } from '@/api/login/login-api';
	import { sm2Encrypt } from '@/utils/smCrypto';
	import { wxLogin,showConfirm  } from '@/utils/common'; 
	
	export default {
		data() {
			return {
				bg,
				logo,
				isAgree: false, // 初始状态为未选中
				loginForm:{
					username: '',
					password: '',
				},
			}
		},
		methods: {
			handleCheckboxChange(event) {
				this.isAgree = event;
			},
			async login(){
				if(!this.isAgree){
					uni.showToast({
						icon: 'none',
						title: '请阅读并同意《用户服务协议》、《隐私政策》',
					});
					return;
				}
				const loginForm = this.loginForm;
				if (!loginForm.username) {
					uni.showToast({
						icon: 'none',
						title: '请输入登录的手机号',
					});
					return;
				}
				if (!loginForm.password) {
					uni.showToast({
						icon: 'none',
						title: '请输入密码',
					});
					return;
				}
				const code = await wxLogin();
				console.log('wxcode:',code);
				try {
					uni.showLoading({ title: '登录中' });
					// 密码加密
					let encryptPasswordForm = Object.assign({}, loginForm,{
						password:sm2Encrypt(loginForm.password),
						code:code
					});
					  
					this.$store.dispatch('Login', encryptPasswordForm).then((res) => {
						console.log('login info:',res);
						uni.showToast({ title: '登录成功' });
						// 设置用户信息
						this.$store.dispatch('GetInfo').then(res => {
						  uni.switchTab({ url: '/pages/mine/index' });
						})
					}).catch((error) => {
						uni.showToast({
							icon: 'none',
							title: error,
							duration:3000
						});
						console.log('e',error);
					})
				} catch (e) {
					console.error('login error',e);
					uni.hideLoading();
				}
			},
			
			nav2MobileLogin(){
				uni.navigateTo({
					url:'/pages/user/mobile-login'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
@import '@/uni.scss';

.top {
	position: relative;
	.info {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}
	
	.logo {
		top: 100rpx;
	}
	
	.product {
		padding: 30rpx 0;
		font-size: 48rpx;
		font-weight: bold;
		color: #fff;
	}
}

.form {
	padding: 60rpx;
	
	.u-input + .u-input {
		margin-top: 30rpx;
	}
	
	.forget-wrap {
		font-size: 28rpx;
		padding: 30rpx 0;
		text-align: right;
		color: $uni-text-color-grey;
	}
	
	.common {
		font-size: 28rpx;
		color: $uni-text-color-grey;
	}
	
	.highlight {
		font-size: 28rpx;
		color: $uni-color-info;
	}
	
	.tourist {
		font-size: 28rpx;
		padding: 20rpx 0;
		text-align: center;
		color: $uni-text-color-grey;
	}
}


</style>
